import re
from src.utils.method import Method


class ProcessLog:
    def __init__(self, raw_text: str):
        """程序输出文本类"""
        self.__raw_text = raw_text.strip("\n").strip("\r")
        self.text = ""
        self.color_text = ""
        self.deal_log()

    def deal_log(self):
        """CQ码屏蔽与颜色处理"""
        self.__raw_text = re.sub("\\[CQ:.+?]", "", self.__raw_text)
        self.text = re.sub("\\x1b\\[[\\d;?]*?[a-zA-Z]", "", self.__raw_text)
        # 处理输出颜色：有颜色保持，无颜色增添
        if re.search("\033\\[.*?m", self.__raw_text):
            self.color_text = self.__raw_text
        else:
            # 为 WARN和 ERROR输出增添颜色
            if re.search("\\[.*INFO.*]", self.__raw_text):
                self.color_text = f"\033[0m{self.__raw_text}"
            elif re.search("\\[.*WARN.*]", self.__raw_text):
                self.color_text = f"\033[33m{self.__raw_text}"
            elif re.search("\\[.*ERR.*]", self.__raw_text):
                self.color_text = f"\033[31m{self.__raw_text}"
            # 中括号开头恢复颜色，其它保持
            elif re.match("\\[", self.__raw_text):
                self.color_text = f"\033[0m{self.__raw_text}"
            else:
                self.color_text = self.__raw_text


class RobotLog:
    def __init__(self, text=""):
        """机器人消息类"""
        self.text = text
        self.length = len(self.text)

    def __add__(self, other):
        tmp = RobotLog()
        if self.length:
            tmp.text = self.text + "\n" + other.text
        else:
            tmp.text = other.text
        tmp.length = self.length + other.length
        return tmp


class PlaceholderHandler:
    def __init__(self):
        self.__placeholder_dict = {}
        self.__re_res = []

    def add_placeholder(self, placeholder: str, value: str):
        """添加占位符"""
        self.__placeholder_dict[placeholder] = value

    def set_re_res(self, re_res: list):
        """正则匹配结果集合"""
        self.__re_res = re_res

    def deal_placeholder(self, text: str) -> str:
        """处理所有占位符"""
        for placeholder in self.__placeholder_dict:
            value = self.__placeholder_dict[placeholder]
            text = text.replace(placeholder, value)
        text = Method.deal_placeholder(text, self.__re_res)
        return text


class RobotEvent:
    def __init__(self, onebot_event: dict):
        """机器人消息"""
        self.post_type: str = onebot_event["post_type"]
        self.notice_type: str = onebot_event.get("notice_type")
        self.message_type: str = onebot_event.get("message_type")
        # 群聊消息处理CQ码
        raw_message: str = onebot_event.get("raw_message")
        if raw_message is not None:
            self.raw_message = Method.deal_cq_tags(raw_message)
        # id确保为int
        self.user_id: int = onebot_event.get("user_id")
        group_id = onebot_event.get("group_id")
        if group_id is not None:
            self.group_id = int(group_id)
        # 实例化发送者
        sender: dict = onebot_event.get("sender")
        if sender is not None:
            self.sender = RobotEventSender(sender)


class RobotEventSender:
    def __init__(self, onebot_sender):
        """发送者信息"""
        self.user_id: int = onebot_sender["user_id"]
        self.nickname: str = onebot_sender["nickname"]
        self.role: str = onebot_sender.get("role", "")
        self.card_name: str = onebot_sender.get("card", "")
        if not self.card_name:
            self.card_name = self.nickname

    def is_admin(self) -> bool:
        """是否为群管"""
        return self.role in ["owner", "admin"]
